package com.feature.shop.front.gateway.filter;

import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

import com.feature.core.constant.SecurityConstants;
import com.feature.shop.front.gateway.config.IgnoreWhiteProperties;

import reactor.core.publisher.Mono;

/**
 * 网关鉴权
 * 
 */
@Component
public class AuthFilter implements GlobalFilter, Ordered {
    private static final Logger log = LoggerFactory.getLogger(AuthFilter.class);

    @Autowired
    private IgnoreWhiteProperties ignoreWhite;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpRequest.Builder mutate = request.mutate();
        mutate.header(SecurityConstants.FROM_SOURCE_REQUEST_HEAD, SecurityConstants.FROM_SOURCE_OUTER);
        String url = request.getURI().getPath();
        // 跳过不需要验证的路径
        if (!ArrayUtils.contains(ignoreWhite.getWhites().toArray(), url)) {
            
        }
        return chain.filter(exchange.mutate().request(mutate.build()).build());
    }

    @Override
    public int getOrder() {
        return -200;
    }
}